package mydatisdb.controller;

import mydatisdb.mapper.ProductMapper;
import mydatisdb.pojo.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Scanner;
@Component
public class ProductController {
    @Autowired
    private ProductMapper productMapper;
    public void start(){
        System.out.println("请选择对应的操作，输入序号：");
        System.out.println("1.添加商品信息，2：查询一个商品，3：删除一个商品 4：查询所有商品");
        Scanner sc=new Scanner(System.in);
        int num=Integer.parseInt(sc.nextLine());
        switch (num){
            case 1:
                inserProduct();
                break;
            case 2:
                System.out.println("请输入商品id");
//                selectSingleProduct();
                break;
            case 3:
                System.out.println("请输入商品id");
//                deleteById();
                break;
            case 4:
                System.out.println("查询所有商品");
//                selectAllProduct();
                break;
            default:
                System.out.println("输入有误");
        }
    }
    public void inserProduct(){
        System.out.println("请输入商品信息：");
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入商品名称,");
        String title=sc.nextLine();
        System.out.println("请输入商品价格,");
        int price=Integer.parseInt(sc.nextLine());
        System.out.println("请输入商品库存,");
        int stock=Integer.parseInt(sc.nextLine());
        Product product =new Product(1,title,price,stock);
        int num=productMapper.insertProduct(product);
        if(product==null){
            System.out.println("商品不存在");
        }else if(num>0){
            System.out.println("商品添加失败");
        }else {
            System.out.println("商品添加成功");
        }
    }
}
